//We are given integer number n, value v (v=0 or 1) and a position p. Write a sequence of operators 
//that modifies n to hold the value v at the position p from the binary representation of n.
//	Example: n = 5 (00000101), p=3, v=1 ? 13 (00001101)
//	n = 5 (00000101), p=2, v=0 ? 1 (00000001)
using System;
class ChangeBits
{
    static void Main()
    {
        int number, position, value;
        while (true)
        {
             Console.WriteLine("Please enter a number:");
            bool isTrueNumber = int.TryParse(Console.ReadLine(), out number);
			Console.WriteLine("Please enter a bit position with positive number:");
            bool isTruePosition = int.TryParse(Console.ReadLine(), out position);
            Console.WriteLine("Please enter a value 1 or 0 to change bit:");
            bool isTrueValue = int.TryParse(Console.ReadLine(), out value);
            if (position>0 && isTrueValue && isTrueNumber && isTruePosition && (value == 0 || value == 1))
            {
                if (value == 0)
                {
                    Console.WriteLine("The number {0} in binary. \nIn integer = {1}", Convert.ToString(number,2).PadLeft(32,'0'),number);
					int result = (~(1 << position) & number);
                    Console.WriteLine("The result is {0} in binary. \nIn integer = {1}", Convert.ToString(result,2).PadLeft(32,'0'),result);
                }
                else
                {
                    Console.WriteLine("Before number was {0} in binary. \nIn integer = {1}",Convert.ToString(number,2).PadLeft(32,'0'),number);
					int result2 = ((1 << position) | number);
					Console.WriteLine("The result is {0} in binary. \nIn integer = {1}",Convert.ToString(result2,2).PadLeft(32,'0'),result2);
                }
				Console.Write("Are you want to continue to joging  with a numbers?(Y/N): ");
				String choice = Console.ReadLine();
				if(choice == "Y")
				{
					continue;
				}
				else if(choice == "N")
				{
					return;
				}
            }
			else
			{
				Console.WriteLine("Something incorect! Input again!");
			}
        }
    }
}